package com.mlm.mlmapp.activity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.interfaces.Prefs_;
import com.mlm.mlmapp.utils.CommonUtils;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
@EActivity(R.layout.modify_pwd)
public class ModifyPWDActivity extends BaseActivity implements HTTPCons {
	AQuery aq;
	AjaxCallback<JSONObject> nickNameCB;
	@StringRes
	String modify_success;
	@Pref
	Prefs_ pref;
	@ViewById
	EditText et_name;
	@ViewById
	EditText et_confirm_name;


	@Click
	void btn_register_action() {
		if (registerValidateEmpty()) {
			registerAction();
		}

	}

	@AfterViews
	void init() {
		String title=getTitle().toString();
		if(!"".equals(title)){
			((TextView) this.findViewById(R.id.tv_title)).setText(title);
		}
		
		aq = new AQuery(this);
		nickNameCB = new AjaxCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject html, AjaxStatus status) {
				if(status.getCode()==-101){
					Toast.makeText(aq.getContext(),"网络异常,请检查网络", Toast.LENGTH_SHORT);
					return;
				}
				if (status.getCode() != 200) {
					Toast.makeText(aq.getContext(), "网络异常:" + status.getCode(),
							Toast.LENGTH_LONG).show();
				} else {
					try {
						if (html.getBoolean("isSuccess")) {
							Toast.makeText(aq.getContext(), modify_success,
									Toast.LENGTH_LONG).show();
							onSuccess();

						} else {
							Toast.makeText(aq.getContext(),
									html.getString("errors"), Toast.LENGTH_LONG)
									.show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		};
	}



	/**
	 * Modify nick name request
	 * 
	 * @throws UnsupportedEncodingException
	 */
	private void registerAction() {
		String username = et_name.getText().toString();
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("mobile", pref.loginID().get()));
		pairs.add(new BasicNameValuePair("passwd", et_name.getText().toString()));
		pairs.add(new BasicNameValuePair("newpasswd", et_confirm_name.getText().toString()));
		CommonUtils.postRequest(modify_pwd_action, aq, nickNameCB, pairs);

	}


	public boolean registerValidateEmpty() {
		List<View> validateList = new ArrayList<View>();
		validateList.add(et_name);
		validateList.add(et_confirm_name);
		return CommonUtils.validateETEmpty(this, validateList);
	}
	void onSuccess() {
		Intent intent = new Intent();
		intent.putExtra("flag", true);
		this.setResult(RESULT_OK, intent);
		this.finish();

	}
	@Click
	void btn_back() {
		onBackPressed();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.putExtra("flag", false);
		this.setResult(RESULT_OK, intent);
		this.finish();
	}
}
